/**
 * Anderground project - Subway maps processor application.
 * Copyright (C) 2008-2009 Eldar Abusalimov, Ilya Shurigyn
 * 
 * This file is part of Anderground.
 * 
 * Anderground is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Anderground is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anderground.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anderground.modules.drawer.rapid;

import java.util.ArrayList;
import java.util.HashMap;

import org.anderground.core.graphics.Canvas;
import org.anderground.core.graphics.PointF;
import org.anderground.core.model.Transition;
import org.anderground.core.model.Tunnel;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
class Link {

	private static final LinkDrawerManager manager = new LinkDrawerManager();

	/** a set of transitions represented in this Link instance */
	private HashMap<PointF, Transition> transitionsMap = new HashMap<PointF, Transition>();

	protected Node evenNode, oddNode;

	/** a set of colors */
	protected ArrayList<Integer> colors = new ArrayList<Integer>();

	protected PointF evenPointF, oddPointF;

	protected boolean smooth;

	public Link(Node evenNode, Node oddNode) {
		this.evenNode = evenNode;
		this.oddNode = oddNode;
		this.evenPointF = evenNode.getPointF();
		this.oddPointF = oddNode.getPointF();
		evenNode.addNeighbor(oddNode, this);
		oddNode.addNeighbor(evenNode, this);
	}

	public void addTransition(Transition transition) {
		for (Transition eachTransition : transitionsMap.values()) {
			if (eachTransition.getFromStation() == transition.getToStation()
					&& eachTransition.getToStation() == transition
							.getFromStation()) {
				if (eachTransition instanceof Tunnel
						&& transition instanceof Tunnel) {
					if (((Tunnel) eachTransition).getLine() == ((Tunnel) transition)
							.getLine()) {
						return;
					}
				}
			}
		}
		this.transitionsMap.put(new PointF(), transition);
		if (transition instanceof Tunnel) {
			Tunnel tunnel = (Tunnel) transition;
			this.smooth = tunnel.isSmooth();
			this.colors.add(tunnel.getLine().getColor());
		}
	}

	public void draw(Canvas canvas) {
		manager.getDrawer().draw(canvas, this);
	}

	public static LinkDrawerManager getManager() {
		return manager;
	}

}
